package tpo.wmb.consumer.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.Area;
import pojo.User;
import pojo.Useraddress;
import tpo.wmb.consumer.service.UserService;
import until.OrderCoderUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 查询客户分页
     * @param pageNum
     * @param name
     * @param region
     * @param controlPage
     * @return
     */
    @RequestMapping("/SelectUserList")
    public PageInfo<User> selectUserList(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) String name,
                                         @RequestParam(required = false) Integer region,@RequestParam(required = false) String controlPage){
        int pageSize = 2 ;
        Map<String,Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("region", region);
        map.put("name", name);
        PageInfo<User> pageInfo = userService.selectUserList(map);
        if("pre".equals(controlPage) && pageInfo.isHasPreviousPage()){
            pageNum -= 1;
        }else if("next".equals(controlPage) && pageInfo.isHasNextPage()){
            pageNum += 1;
        }
        map.put("pageNum",pageNum);
        return userService.selectUserList(map);
    }

    //删除客户
    @RequestMapping("/DeleteUser")
    public Integer deleteUser(@RequestParam Integer id){
        return userService.deleteUser(id);
    }

    //修改客户
    @RequestMapping("UpdateUser")
    public Integer updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }


    //新增客户 userNumber
    @RequestMapping("/InsertUser")
    public Integer insertUser(@RequestBody User user){
        user.setUserNumber(OrderCoderUtil.getYonghuCode(null));
        return userService.insertUser(user);
    }


    //查询客户
    @RequestMapping("/SelectUser")
    public User selectUser(@RequestParam Integer id){
        return userService.selectUser(id);
    }


    //查询默认地址
    @RequestMapping("/SelectDefalutAddress")
    public Useraddress selectDefalutAddress(@RequestParam Integer uid){
        return userService.selectDefalutAddress(uid);
    }


    //
    @RequestMapping("/SelectUsers")
    public List<User> selectUsers(){
        return userService.selectUsers();
    }


    //查询用户所有地址
    @RequestMapping("/SelectUseraddressList")
    public List<Useraddress> selectUseraddressList(@RequestParam Integer uid){
        return userService.selectAddressList(uid);
    }


    //添加地址
    @RequestMapping("/InsertAddress")
    public Integer insertAddress(@RequestBody Useraddress useraddress){
        return userService.insertAddress(useraddress);
    }


    @RequestMapping("/DeleteAddress")
    public Integer deleteAddress(@RequestParam Integer id){
        return userService.deleteAddress(id);
    }


    @RequestMapping("/UpdateAddress")
    public Integer updateAddress(@RequestBody Useraddress useraddress){
        return userService.updateAddress(useraddress);
    }

    /**
     * 根据用户选择的区域查询对应的省份集合
     * @return
     */
    @RequestMapping("/SelectUserArea")
    List<Area> selectUserArea(@RequestParam Integer upperLevelID){
        return userService.selectUserArea(upperLevelID);
    }

}
